﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
	private Transform target;

	[Header("子弹飞行速度")]
	public float moveSpeed;

	[Header("子弹的伤害值")]
	public float hurt;

	private bool isMove = false;    // 是否在射击
	public bool IsMove { get => isMove; }

	public void StartMove(Transform attackTarget, float power) { // 子弹开始向攻击目标移动
		target = attackTarget;
		hurt = power;
		isMove = true;
	}

	private void Update() {
		if (isMove && target) {
			transform.LookAt(target.position + new Vector3(0, 1.7f, 0));
			transform.Translate(
				(target.position + new Vector3(0, 1.7f, 0) - transform.position).normalized * Time.deltaTime * moveSpeed,
				Space.World
			);

			if (Vector3.Distance(target.position + new Vector3(0, 1.7f, 0), transform.position) <= 0.1f) {
				isMove = false;  // 停止射击
				target.GetComponent<Enemy>().hurts.Enqueue(hurt);
				Destroy(this.gameObject);
			}
		}

		if (isMove && !target) {    // 怪物突然死了
			isMove = false;
			Destroy(this.gameObject);
		}
	}
}
